package com.koron.css2.newProjectCenter.impl;

import cn.hutool.core.util.ObjectUtil;
import com.koron.css2.baseConfig.mapper.ConfigMapper;
import com.koron.css2.clientCenter.bean.UserBean;
import com.koron.css2.clientCenter.mapper.UserInfoMapper;
import com.koron.css2.common.dto.AttachmentDto;
import com.koron.css2.common.service.AbstractExportExcel;
import com.koron.css2.meterService.dto.MsInfoDto;
import com.koron.css2.meterService.mapper.MsInfoMapper;
import com.koron.css2.newProjectCenter.bean.PjInfoBean;
import com.koron.css2.newProjectCenter.bean.PjMeter;
import com.koron.css2.newProjectCenter.bean.query.PjMeterQuery;
import com.koron.css2.newProjectCenter.mapper.PjInfoMapper;
import com.koron.css2.newProjectCenter.mapper.PjMeterMapper;
import com.koron.css2.newProjectCenter.vo.PjMeterExportVO;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.css2.systemManage.bean.OrgUserBean;
import com.koron.css2.systemManage.bean.OrgUserQueryBean;
import com.koron.css2.systemManage.mapper.OrgUserMapper;
import com.koron.util.DateUtils;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

public class PjMeterInstallBuildExport extends AbstractExportExcel {
    //判断参数【是否允许水表出库环节编辑用户编号  0是 1否】
    private static final String PJ_METER_INSTALL = "PJ_METER_INSTALL";
    //允许水表出库环节编辑用户编号
    private static final String CONFIG_OPEN="0";

    @Override
    public AttachmentDto getAttachment(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        PjMeterQuery pjMeterQuery = JsonUtils.objectToPojo(req.getData(), PjMeterQuery.class);
        PjMeterMapper pjMeterMapper = factory.getMapper(PjMeterMapper.class);
        OrgUserMapper orgUserMapper = factory.getMapper(OrgUserMapper.class);
        UserInfoMapper userInfoMapper = factory.getMapper(UserInfoMapper.class);
        MsInfoMapper msInfoMapper = factory.getMapper(MsInfoMapper.class);
        PjInfoMapper pjInfoMapper = factory.getMapper(PjInfoMapper.class);
        //获取配置表mapper
        ConfigMapper configMapper = factory.getMapper(ConfigMapper.class);
        //获取参数【是否允许水表出库环节编辑用户编号  0是 1否】配置
        String config = configMapper.getConfigValueByName(PJ_METER_INSTALL);
        // 水表安装人员信息MAP
        Map<String, OrgUserBean> meterBuilderMap = null;
        // 水表信息MAP
        Map<String, MsInfoDto> msInfoMap = null;
        List<PjMeter> pjMeterList = new ArrayList<>();
        PjInfoBean pjInfo = new PjInfoBean();
        // 1. 查出所有水表信息
        if (StringUtils.isNotBlank(pjMeterQuery.getConsultId())) {
            pjInfo = pjInfoMapper.selectById(pjMeterQuery.getConsultId());
            pjMeterList = pjMeterMapper.getList(pjMeterQuery);
        }
        List<PjMeterExportVO> pjMeterExportVOList = new ArrayList<>();


        // 3. 获取对应的导出数据
        if (pjMeterList != null && pjMeterList.size() > 0) {
            // 用户信息主键ID集合
            List<String> userInfoTempIdList = pjMeterList.stream().map(PjMeter::getUserInfoTempId).collect(Collectors.toList());
            // 水表信息主键ID集合
            List<String> msInfoIdList = pjMeterList.stream().map(PjMeter::getMsInfoId).collect(Collectors.toList());
            // 查询出用户信息
            List<UserBean> userBeanList = userInfoMapper.selectListByIds(userInfoTempIdList, UserBean.USERTEMP);

            // 用户信息中的水表安装人员转换成list
            List<String> meterBuilderList = userBeanList.stream().filter(t -> StringUtils.isNotEmpty(t.getMeterBuilder())).map(t -> t.getMeterBuilder()).collect(Collectors.toList());
            if (meterBuilderList.size() > 0){
                OrgUserQueryBean orgUserQuery = new OrgUserQueryBean();
                orgUserQuery.setUserAccounts(meterBuilderList);
                // 获取水表安装人员信息
                List<OrgUserBean> orgUserList = orgUserMapper.selectByAccounts(orgUserQuery);
                // 将水表安装人员信息转化成 MAP
                meterBuilderMap = orgUserList.stream().collect(Collectors.toMap(OrgUserBean::getAccount, Function.identity()));
            }

            // 水表信息转成MAP
            if (msInfoIdList.size() > 0){
                List<MsInfoDto> msInfoList = msInfoMapper.getMsInfoByIds(msInfoIdList);
                msInfoMap = msInfoList.stream().collect(Collectors.toMap(MsInfoDto::getId, Function.identity()));
            }

            // 人员信息转成MAP
            Map<String, UserBean> userGroup = userBeanList.stream().collect(Collectors.toMap(UserBean::getId, Function.identity()));
            for(PjMeter pjMeter : pjMeterList){
                PjMeterExportVO pjMeterExportVO = new PjMeterExportVO();
                UserBean userBean = userGroup.get(pjMeter.getUserInfoTempId());
                // 户号
                pjMeterExportVO.setUserNo(userBean.getUserNo());
                // 户名
                pjMeterExportVO.setCtmName(userBean.getCtmName());
                // 装表地址
                pjMeterExportVO.setSetupMeterAddr(userBean.getSetupMeterAddr());
                // 水表安装人员
                if (meterBuilderMap != null){
                    pjMeterExportVO.setMeterBuilder(meterBuilderMap.get(userBean.getMeterBuilder()).getUserName());
                }
                // 表位
                pjMeterExportVO.setMeterSite(userBean.getMeterSite());
                // 表井模式
                String meterBjms = userBean.getMeterBjms();
                if (StringUtils.isNotEmpty(meterBjms)) {
                    pjMeterExportVO.setMeterBjmsName(Tools.getDicNameByCodeAndValue(factory, "BJMS", userBean.getMeterBjms()));
                } else {
                    pjMeterExportVO.setMeterBjmsName(meterBjms);
                }
                // 4.5 meterBjxsName
                String meterBjxs = userBean.getMeterBjxs();
                if (StringUtils.isNotEmpty(meterBjxs)) {
                    pjMeterExportVO.setMeterBjxsName(Tools.getDicNameByCodeAndValue(factory, "BJXS", userBean.getMeterBjxs()));
                } else {
                    pjMeterExportVO.setMeterBjxsName(meterBjxs);
                }
                // 最后抄码
                pjMeterExportVO.setLastReadingNum(userBean.getLastReadingNum());
                // 水表安装日期
                pjMeterExportVO.setSetupMeterDate(userBean.getSetupMeterDate());
                // 集中器
                pjMeterExportVO.setConcentratorNo(userBean.getConcentratorNo());
                // 总表编号
                pjMeterExportVO.setFatherMeterNo(userBean.getFatherMeterNo());
                // 计划安装日期
                Date planMeterInstallDate = pjMeter.getPlanMeterInstallDate();
                if (planMeterInstallDate != null) {
                    pjMeterExportVO.setPlanMeterInstallDate(DateUtils.parseDate(planMeterInstallDate, "yyyy-MM-dd"));
                } else {
                    pjMeterExportVO.setPlanMeterInstallDate("");
                }

                // 设置水表表身码
                String msInfoId = pjMeter.getMsInfoId();
                if (StringUtils.isNotEmpty(msInfoId) && msInfoMap.get(msInfoId) != null) {
                    pjMeterExportVO.setFactoryNo(msInfoMap.get(msInfoId).getFactoryNo());
                } else {
                    pjMeterExportVO.setFactoryNo("");
                }

                // 用户信息主键ID
                pjMeterExportVO.setUserInfoTempId(userBean.getId());
                // 用水类型
                pjMeterExportVO.setUserWaterTypeName(userBean.getUseWaterTypeName());
                pjMeterExportVOList.add(pjMeterExportVO);
            }
        }

        AttachmentDto attachment = new AttachmentDto();
        if (ObjectUtil.isNotNull(pjInfo)&&Objects.equals("build", pjInfo.getProcessState())){
            attachment.setBtlTemplate("PjMeterBuildPlanExport.btl");
            attachment.setFileTemplate("PjMeterBuildPlanExport.xlsx");
        }else if (CONFIG_OPEN.equals(config)){
            attachment.setBtlTemplate("MzPjMeterInstallBuildExport.btl");
            attachment.setFileTemplate("MzPjMeterInstallBuildExport.xlsx");
        }else{
            attachment.setBtlTemplate("PjMeterInstallBuildExport.btl");
            attachment.setFileTemplate("PjMeterInstallBuildExport.xlsx");
        }

        attachment.setData(pjMeterExportVOList);
        attachment.setFilename("工程报装-工程施工-水表导出模板");

        attachment.setFileType("excel");
        return attachment;
    }
}
